﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Authorization.Users;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.MultiTenancy;
using Castle.Components.DictionaryAdapter;
using XHD.WXZJ.Posts;

namespace XHD.WXZJ.Departments
{
  public  class DepartmentStore<TTenant,TUser,TDepartment,TPost>:
       ITransientDependency
        where TTenant : AbpTenant<TTenant, TUser>
        where TDepartment : AbpDepartment<TTenant, TUser>
        where TUser : AbpUser<TTenant, TUser>
        where TPost : Posts<TTenant, TDepartment, TUser>
    {
       
        private readonly IRepository<TPost, int> _postRepository;
        private readonly IRepository<TDepartment, int> _departmentRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

      /// <summary>
      /// 构造函数
      /// </summary>
      /// <param name="postRepository"></param>
      /// <param name="departmentRepository"></param>
      /// <param name="unitOfWorkManager"></param>
      public DepartmentStore(IRepository<TPost, int> postRepository,
          IRepository<TDepartment, int> departmentRepository,
          IUnitOfWorkManager unitOfWorkManager)
      {
          _departmentRepository = departmentRepository;
          _postRepository = postRepository;
          _unitOfWorkManager = unitOfWorkManager;
      }
      /// <summary>
      /// 获取所有部门
      /// </summary>
      public virtual IQueryable<TDepartment> Departments
      {
          get { return _departmentRepository.GetAll(); }
      }
      /// <summary>
      /// 创建部门
      /// </summary>
      /// <param name="department"></param>
      /// <returns></returns>
      public virtual async Task CreateAsync(TDepartment department)
      {
           await _departmentRepository.InsertAsync(department);
      }
      /// <summary>
      /// 更新部门
      /// </summary>
      /// <param name="department"></param>
      /// <returns></returns>
      public virtual async Task UpdateAsync(TDepartment department)
      {
          await _departmentRepository.UpdateAsync(department);
      }
      /// <summary>
      /// 删除部门
      /// </summary>
      /// <param name="department"></param>
      /// <returns></returns>
      public virtual async Task DeleteAsync(TDepartment department)
      {
      
        
              var post = await _postRepository.FirstOrDefaultAsync(p => p.DepartmentId == department.Id);
          if (post != null)
          {
             await _postRepository.DeleteAsync(post);
          }


          await _departmentRepository.DeleteAsync(department);
      }
      /// <summary>
      /// 通过部门ID查找部门
      /// </summary>
      /// <param name="departmentId"></param>
      /// <returns></returns>
      public virtual async Task<TDepartment> FindByIdAsync(int departmentId)
        {
            return await _departmentRepository.FirstOrDefaultAsync(departmentId);
        }

      /// <summary>
      /// 通过部门名称查找部门
      /// </summary>
      /// <param name="departmentName"></param>
      /// <returns></returns>
      public virtual async Task<TDepartment> FindByNameAsync(string departmentName)
        {
            return await _departmentRepository.FirstOrDefaultAsync(
                department => department.Name == departmentName
                );
        }
      /// <summary>
      /// 根据现实名称查找部门
      /// </summary>
      /// <param name="displayName"></param>
      /// <returns></returns>
      public virtual async Task<TDepartment> FindByDisplayNameAsync(string displayName)
        {
            return await _departmentRepository.FirstOrDefaultAsync(
                department => department.DisplayName == displayName
                );
        }
      /// <summary>
      /// 获取部门下所有岗位名称
      /// </summary>
      /// <param name="department"></param>
      /// <returns></returns>
      public virtual Task<IList<string>> GetPostsNameAsync(TDepartment department)
      {
          var departmentNames = _postRepository.Query(posts => (from post in posts
              join dp in
                  _departmentRepository.GetAll() on post.DepartmentId equals dp.Id
              where post.DepartmentId == department.Id
              select post.Name).ToList());
          return Task.FromResult<IList<string>>(departmentNames);
      }
      /// <summary>
      /// 获取部门下所有岗位实体
      /// </summary>
      /// <param name="department"></param>
      /// <returns></returns>
      public virtual Task<IList<TPost>> GetDepartmentPostsAsync(TDepartment department)
      {
          var dpPosts = _postRepository.Query(posts => (from post in posts
                                                                join dp in
                                                                    _departmentRepository.GetAll() on post.DepartmentId equals dp.Id
                                                                where post.DepartmentId == department.Id
                                                                select post).ToList());
          return Task.FromResult<IList<TPost>>(dpPosts);
      }
    }
}
